<html>
<head>
<?php 
	session_start();
	if(empty($_SESSION['loginuser'])){
		header("Location: login.php");
		exit();		
	}
?>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<link href="css/bootstrap.min.css" rel="stylesheet">
<style>

.content{border:0px solid #ddd;width:405px;}
.datetable{border-top:1px solid #ccc;border-left:1px solid #ccc;background:#fff;}
.datetable td{border-bottom:1px solid #ccc;border-right:1px solid #ccc;text-align:center;}
.datetable thead{background:#006600;}
.datetable thead td{padding:10px 5px;font:normal 12px/normal 'microsoft yahei';color:#000000;text-align:center;}
.datetable thead td span{padding:0 5px;}
.datetable tbody td{padding:5px 3px;font-size:12px;}
</style>
<script type="text/javascript" src="./js/myjs.js"></script>
<script type="text/javascript" src='./js/jquery-1.11.2.min.js'></script>
<script type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap.js"></script>
<script type="text/javascript" src="js/npm.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		//获取每日心语
		// $.get("getApiController.php?type=zhaiyan" ,function(data, status){
		// 	if(data.length == 0 || data['errNum'] > 0)
		// 	{
		// 		$('#pWeather').html("无法获取心语，请检查！");
		// 	}else{
		// 	}
		// },
		// "json");
	})

	function changeColor(val,obj){
		if(val == 'over'){
			obj.style.color="red";
		}else if(val == "out"){
			obj.style.color="black";
		}
	}

	function openChatRoom(obj){
		window.open("chatRoom.php?username_chat="+obj.innerHTML,"_blank");
	}
	
	function getUserList(){
		$.post("getUserListController.php",
			{
				username:"<?php echo $_SESSION['loginuser'];?>"
			},
			function(data, status){
				for(var i=0; i<data['userlist'].length; i++)
				{
					var newNode = document.createElement("li");
					newNode.innerHTML='<code>' + data['userlist'][i].username + '</code>';
					newNode.setAttribute("onmouseover","changeColor('over',this)");
					newNode.setAttribute("onmouseout","changeColor('out',this)");
					newNode.setAttribute("onclick","openChatRoom(this)");
					$('#ul1').append(newNode);
				}
				if(data['unreadmsg'].length == 0)
				{
					//alert('aa');
					$('#unread').text(function(i, origText){
						return origText + "0 条";
					});
				}else{
					for(var j=0; j<data['unreadmsg'].length; j++){
						$('#unread').text(function(i, origText){
							return origText +"来自"+data['unreadmsg'][i].sender+"有"+data['unreadmsg'][i].count+"条信息 | "
						});
					}
				}
			}, "json");
		initial();
		getSignTimes();
		getTopLate();
		//获取天气数据和空气质量数据
		getWeatherData();
	}

	function getSignTimes()
	{
		var current = $('#SY').val() + '-' + $('#SM').val();
		$.get("signController.php?act=countsigns&current="+current,function(data, status){
			for(var i=0;i<42;i++) {
				sObj=$('#SD'+ i);
				gObj = $("#GD"+i);
				for(var j=0; j<data.length; j++)
				{
					if(data[j].dayOfMonth == sObj.html()){
						if(data[j].isLate == 1) {
							sObj.html(function(i, origText){
								return origText + "签";
							});
							gObj.css("backgroundColor","#fb0");
						}
						if(data[j].isLate == 0) {
							sObj.html(function(i, origText){
								return origText + "迟";
							});
							gObj.css("backgroundColor","#afeeee");
						}
						
					}
				}
				//sObj.innerHTML);
			}
		},
		"json");
	}

	function getWeatherData()
	{
		var cityname = "贵阳";
		$.get("getApiController.php?type=weather&cityname=" + cityname ,function(data, status){
			$('#pWeather').html(data);			
		}, "html");
	}

	function getTopLate()
	{
		$.get("signController.php?act=topLate",function(data, status){
			for(var j=0; j<data.length; j++)
			{
				var newNode = document.createElement("tr");
				newNode.innerHTML='<th scope="row">' + (j+1) + '</th>'
					+ '<td>' + data[j].userid + '</td>' 
					+ '<td>' + data[j].times + '</td>'
					+ '</tr>';
				$('#topLate').append(newNode);

			}

		},
		"json");
	}
</script>
</head>
<div class="container">
<body onload="getUserList()">
<h2>好友列表[<?php echo $_SESSION['loginuser'];?>]&nbsp;&nbsp;&nbsp;&nbsp;<a href="loginController.php?type=logout">退出登录</a>
<br/>
</h2>
<div class= "row">
<div class= "col-md-6">
<form action="signController.php" method="post" style="margin:15px 0px" class="form-horizontal">
<div class="form-group has-success">
	<div class="col-sm-2">
		<label class="control-label" for="formGroupInputLarge">今日心情:</label>
	</div>
    <div class="col-sm-8">
      <input class="form-control" type="text" id="xinqing" name="comment" value="今天心情爽翻了！" placeholder="今天心情爽翻了！"/>
    </div>
    <div class="col-sm-2">
    	<button type="submit" class="btn btn-success">签到</button>
    </div>
</div>
</form>
<span id="pWeather" style="background:#ebc79e; margin:15px, 0px; padding:5px;"></span>
<br/>
<hr/>
<p id="unread">点击以下列表即可聊天，未读消息：</p>
<ul id="ul1">
</ul>
<hr/>
</div>
<div class="col-md-4">
<table class="table table-hover">
      <thead>
        <tr>
          <th>本月Top7</th>
          <th>迟到人员</th>
          <th>迟到次数</th>
        </tr>
      </thead>
      <tbody id="topLate">
      </tbody>
    </table>


<div class = "content">
<form name="CLD" class="content">
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="datetable">
    <thead>
    <tr>
      <td colSpan=7><span>公历</span>
        <select id="SY" name="SY" onchange="changeCld();getSignTimes();" style="font-SIZE: 9pt">
        <script>
            for(i=1900;i<2050;i++) document.write('<option>'+i);
        </script>
         </select><span>年</span>
         <select id="SM" name="SM" onchange="changeCld();getSignTimes();" style="font-SIZE: 9pt">
        <script>
            for(i=1;i<13;i++) document.write('<option>'+i);
        </script>
        </select><span>月</span>
        </font><span id="GZ"></span>
      </td>
    </tr>
    </thead>
    <tbody>
    <tr style="background:#eee;">
      <td width="54">日</td>
      <td width="54">一</td>
      <td width="54">二</td>
      <td width="54">三</td>
      <td width="54">四</td>
      <td width="54">五</td>
      <td width="54">六</td>
    </tr>            
    <script>
    var gNum;
    for(i=0;i<6;i++) {
       document.write('<tr align="center">');
       for(j=0;j<7;j++) {
          gNum = i*7+j;
          document.write('<td id="GD' + gNum +'"><font id="SD' + gNum +'" size=2 face="Arial Black"');
          if(j == 0) document.write('color="red"');
          if(j == 6) document.write('color="#000080"');
          document.write('></font><br/><font id="LD' + gNum + '" size=2 style="font-size:9pt"></font></td>');
       }
       document.write('</tr>');
    }
   </script>
   </tbody>
</table>
</form>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">


</div>
</div>

</body>
</div>
</html>
